const userName = document.getElementsByName("userName")[0];
const passWord = document.getElementsByName("passWord")[0];
const landing = document.getElementsByName("landing")[0];
const enroll = document.getElementsByName("enroll")[0];
const alertBox = document.querySelector(".alertBox");
const alertBoxText = document.querySelector(".alertBox .text");
const alertBoxX = document.querySelector(".alertBox .x");

// 相应拦截
axios.interceptors.response.use((res) => {
    // console.log(res);
    return res.data;
});

console.log(alertBox);
// 小弹框叉叉
alertBoxX.onclick = function () {
    alertBox.style.display = "none";
};

// 注册键操作
enroll.onclick = function () {
    userNameValue = userName.value;
    passWordValue = passWord.value;
    axios({
        url: "/enroll",
        method: "post",
        data: { userNameValue, passWordValue },
    }).then(({ ok, msg, err }) => {
        if (ok === 1) {
            console.log(msg);
        } else {
            console.log(msg, err);
            alertBox.style.display = "flex";
            if (err._message === "userList validation failed") {
                alertBoxText.innerHTML = "用户名、密码不能为空";
            } else if (err.code === 11000) {
                alertBoxText.innerHTML = "用户名以存在";
            } else {
                alertBoxText.innerHTML = "未知错误";
            }
        }
    });
};


// 登陆键操作
enroll.onclick = function () {
    userNameValue = userName.value;
    passWordValue = passWord.value;
    axios({
        url: "/landing",
        method: "post",
        data: { userNameValue, passWordValue },
    }).then(({ ok, msg, err }) => {
        // if (ok === 1) {
        //     console.log(msg);
        // } else {
        //     console.log(msg, err);
        //     alertBox.style.display = "flex";
        //     if (err._message === "userList validation failed") {
        //         alertBoxText.innerHTML = "用户名、密码不能为空";
        //     } else if (err.code === 11000) {
        //         alertBoxText.innerHTML = "用户名以存在";
        //     } else {
        //         alertBoxText.innerHTML = "未知错误";
        //     }
        // }
    });
};
